import request from '../request';
import type {
  AccountBalance,
  RechargeRequest,
  RechargeResponse,
  RechargeRecordsResponse,
  BalanceRecordsResponse,
  BalanceRecordsQuery,
  FreezeBalanceRequest,
  FreezeBalanceResponse,
  UnfreezeBalanceRequest,
  UnfreezeBalanceResponse,
  RefundToBalanceRequest,
  RefundToBalanceResponse
} from '@/types/account/Account';

/**
 * 获取账户余额
 */
export async function getAccountBalance() {
  const res = await request<AccountBalance>({
    url: '/account/balance',
    method: 'GET',
    retry: 2
  });
  return res.data;
}

/**
 * 充值
 */
export async function rechargeAccount(data: RechargeRequest) {
  const res = await request<RechargeResponse>({
    url: '/account/recharge',
    method: 'POST',
    data
  });
  return res.data;
}

/**
 * 获取充值记录
 */
export async function getRechargeRecords(params?: { page?: number; limit?: number }) {
  const res = await request<RechargeRecordsResponse>({
    url: '/account/recharge-records',
    method: 'GET',
    params,
    retry: 2
  });
  console.log("res.data"+JSON.stringify(res.data));
  return res.data;
}

/**
 * 获取余额变动明细
 */
export async function getBalanceRecords(params?: BalanceRecordsQuery) {
  const res = await request<BalanceRecordsResponse>({
    url: '/account/balance-records',
    method: 'GET',
    params,
    retry: 2
  });
  console.log("res.data"+JSON.stringify(res.data));
  return res.data;
}

/**
 * 冻结余额
 */
export async function freezeBalance(data: FreezeBalanceRequest) {
  const res = await request<FreezeBalanceResponse>({
    url: '/account/balance/freeze',
    method: 'POST',
    data
  });
  return res.data;
}

/**
 * 解冻余额
 */
export async function unfreezeBalance(data: UnfreezeBalanceRequest) {
  const res = await request<UnfreezeBalanceResponse>({
    url: '/account/balance/unfreeze',
    method: 'POST',
    data
  });
  return res.data;
}

/**
 * 余额返还
 */
export async function refundToBalance(data: RefundToBalanceRequest) {
  const res = await request<RefundToBalanceResponse>({
    url: '/account/balance/refund',
    method: 'POST',
    data
  });
  return res.data;
}
/**
 * 检查充值订单状态
 */
export async function checkRechargeStatus(rechargeNo: string) {
  const res = await request<{
    status: 'SUCCESS' | 'FAILED' | 'PENDING' | 'PROCESSING';
  }>({
    url: `/account/recharge/${rechargeNo}/status`,
    method: 'GET',
    retry: 2
  });
  return res.data;
}

/**
 * 取消充值订单
 */
export async function cancelRecharge(rechargeNo: string) {
  const res = await request<{
    success: boolean;
  }>({
    url: `/account/recharge/${rechargeNo}/cancel`,
    method: 'POST'
  });
  return res.data;
}
/**
 * 主动查询支付状态（调用支付宝接口确认真实支付状态）
 */
export async function checkPaymentStatus(rechargeNo: string) {
  const res = await request<{
    data: {
      status?: 'SUCCESS' | 'PENDING' | string;
    };
    code: number;
    message: string;
    success: boolean;
  }>({
    url: '/api/pay/check-status', 
    method: 'GET',
    params: { rechargeNo },
    retry: 2
  });
  return res.data;
}